package my.merborn.ordersysteam105.controllers;

import lombok.Data;
import my.merborn.ordersysteam105.models.dao.MenuMapper;
import my.merborn.ordersysteam105.models.pojo.Menu;
import my.merborn.ordersysteam105.models.pojo.OrderT;
import my.merborn.ordersysteam105.models.services.MenuService;
import my.merborn.ordersysteam105.models.services.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BackGroundController {
    @Autowired
    OrderService os;
    @Autowired
    MenuMapper mm;
    @Autowired
    MenuService ms;

    @RequestMapping("/background.html")
    public ModelAndView  backGroundPage( ){
        List<OrderT> orderList = os.selectNoPayedAndNoFinished();
        List<OrderTable> orderTableList = new ArrayList<>();
        ModelAndView mv = new ModelAndView("background.html");
        if(orderList.size()==0) return mv;//如果从数据库中拿出的订单表为空，就返回空mv
        for (OrderT o : orderList) {
            List<Menu> foodList = new ArrayList<>();
            OrderTable table = new OrderTable();
            table.setOrder(o);
            String listString = o.getMenuList();
            String[] split = listString.split("@");
            for (String s : split) {
                Menu food = mm.selectByPrimaryKey(Integer.parseInt(s));
                foodList.add(food);
            }
            table.setFoodList(foodList);
            orderTableList.add(table);
        }
        mv.addObject("orderTableList",orderTableList);

/*
        System.out.println("\nfoodList:");
        for (OrderTable t : orderTableList) {
            System.out.println("order ="+t.getOrder().getoId());

            for ( Menu food: t.getFoodList()) {
                System.out.println(food);
            }
        }
        System.out.println("- end -\n");
 */
        return mv;
    }

    /**
     *返回字符串中的菜单对象List
     * @param data
     * @return ArraryList<Menu> List
     */
    public List<Menu> getFoodList(String data) {
        List<Menu> foodList = new ArrayList<>();
        String[] split = data.split("@");
        for (String s : split) {
            Menu food = mm.selectByPrimaryKey(Integer.parseInt(s));
            foodList.add(food);
        }
        return foodList;
    }

    @RequestMapping("/backgroundfinish.do")
    public String  backGroundFinishOrder(@RequestParam String oid){
        OrderT o = new OrderT();
        o.setoId(Integer.parseInt(oid));
        os.finishIt(o);
        return "redirect:background.html";
    }

    @RequestMapping("/rankinglist.html")
    public ModelAndView getMenuRankingList(){
        ModelAndView mv = new ModelAndView("rankinglist.html");
        List<Menu> menus = ms.MenuRankingListOrderByDESC();
        mv.addObject("rankinglist",menus);
        return mv;
    }


    //用于包装订单和食物详情的类
    @Data
    public class OrderTable{
        private OrderT order;
        private List<Menu> foodList;
        public OrderTable(OrderT order,List<Menu> foodList){
                this.order = order;
                this.foodList = foodList;
        }

        public OrderTable(){}
    }




}
